<?php

/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */
namespace Resources\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Login\Form\LoginForm;
use Resources\Form\SessionForm;
use Resources\model\Session;
use Zend\View\Model\JsonModel;

class SessionController extends AbstractActionController
{
    /*function and variables*/
    protected $form;
    
    protected $authservice;
    
    protected $userinfo;
    
    protected $sessionTable;
    
    protected $speakerTable;
    
    private function processData($data) {
        $data['ID']=$this->generateID();
        return $data;
    }
    
    private function generateID(){
        $table_key = '1010';
        $key = $table_key.time();
        return $key;
    }
    
    public function getAuthService()
    {
        if (! $this->authservice) {
            $this->authservice = $this->getServiceLocator()->get('AuthService');
        }
        return $this->authservice;
    }
    
    public function getUserInfo()
    {
        if ($this->getAuthService()->getIdentity()) {
            $this->userinfo = $this->getAuthService()->getStorage()->read();
        }
        return $this->userinfo;
    }
    
    public function getSpeakerTable()
    {
        if(! $this->speakerTable) {
            $this->speakerTable = $this->getServiceLocator()->get('Resource\Model\SpeakerTable');
        }
        return $this->speakerTable;
    }
    
    public function getSessionTable()
    {
        if(! $this->sessionTable) {
            $this->sessionTable = $this->getServiceLocator()->get('Resource\Model\SessionTable');
        }
        return $this->sessionTable;
    }
    
    protected function createSession(array $data,$type)
    {
        $session=new Session();
        $session->exchangeArray($data);
        $this->getSessionTable()->saveSession($session,$type);
        return true;
    }
    
    /*Actions*/
    public function rolecomboAction() {
         
        if ($this->getAuthService ()->getIdentity ()) {
            if(isset($_POST['textData']) && !($_POST['textData']==0))
            {
                $id = $_POST['textData'];
                $result=$this->getSessionTable()->getSession($id);
                 
              return new JsonModel([
                    'title'=>$result->DURATION,
                ]);
            }
        }
        return $this->redirect ()->toRoute ( 'Login' ); 
    }
    
    //Session Object ( [ID] => 10101427742640 [TOPIC] => session 1 [SUMMIT_YEAR] => 2014 [MEDIUM] => 3 [DURATION] => 123 [SECTOR] => 2 [OVERVIEW] => qeqwewq [REMARK] => wewe [FK_SPEAKER_ID] => 10111427738612 ) 
    public function indexAction()
    {
        if ($this->getAuthService()->getIdentity()) {
            //  $form = new RegisterForm();
            $userinfo = $this->getUserInfo();
            $this->layout()->role= $userinfo->ROLE;
            $this->layout()->user=$userinfo->USERNAME;
    
            if (! $userinfo->ROLE == 'admin') {
                return $this->redirect()->toRoute('Login');
            }
            
            $speaker_list=$this->getSpeakerTable()->fetchAll();
            
            $paginator = $this->getSessionTable()->fetchAll(true);
            $paginator->setCurrentPageNumber((int) $this->params()->fromQuery('page', 1));
            $paginator->setItemCountPerPage(10);
    
            $viewmodel=new ViewModel(array(
                'paginator' => $paginator,
                'speaker_list' =>$speaker_list
            ));
            return $viewmodel;
        }
        return $this->redirect()->toRoute('Login');
    }
    
    public function addAction()
    {
        if ($this->getAuthService()->getIdentity()) {
                        
            $userinfo = $this->getUserInfo();
            $this->layout()->role= $userinfo->ROLE;
            $this->layout()->user=$userinfo->USERNAME;

            $form = new SessionForm();
            $speaker_list=$this->getSpeakerTable()->fetchAll();
            
            $viewmodel=new ViewModel(array(
                'form' => $form,
                'id' => $this->params()->fromRoute('id'),
                'speaker_list' =>$speaker_list
            ));
    
            return $viewmodel;
    
        }else {
            return $this->redirect()->toRoute('Login');
        }
    }
    
    public function processAction()
    {
        if ($this->getAuthService()->getIdentity()) {
    
            $userinfo = $this->getUserInfo();
            $this->layout()->role= $userinfo->ROLE;
            $this->layout()->user=$userinfo->USERNAME;
            

    
            if (! $this->request->isPost()) {
                return $this->redirect()->toRoute(NULL, array(
                    'controller' => 'Session',
                    'action' => 'add'
                ));
            }

            $speaker_list=$this->getSpeakerTable()->fetchAll();
            
            $post = $this->request->getPost();
            $form = new SessionForm();    
            $form->setData($post);
            
            if (! $form->isValid()) {
                $model = new ViewModel(array(
                    'form' => $form,
                    'id' => $this->params()->fromRoute('id'),
                    'speaker_list' =>$speaker_list
                ));
                $model->setTemplate('resources/session/add');
                return $model;
            }
    
            $data=$form->getData();
            if(!isset($data['ID'])||$data['ID']==0){
                $data = $this->processData($data);
                $this->createSession($data,0);
            }
            else{
               $this->createSession($data,1);
            }  
            
            return $this->redirect()->toRoute('resources');
    
        }else {
            return $this->redirect()->toRoute('Login');
        }
    }
    
    public function editAction()
    {
        if ($this->getAuthService()->getIdentity()) {
            $form = new SessionForm();
    
            $userinfo = $this->getUserInfo();
            $this->layout()->role= $userinfo->ROLE;
            $this->layout()->user=$userinfo->USERNAME;
    
            $session = $this->getSessionTable()->getSession($this->params()->fromRoute('id'));
            $form->bind($session);
            
            $speaker_list=$this->getSpeakerTable()->fetchAll();
            
            $viewModel = new ViewModel(array(
                'form' => $form,
                'id' => $this->params()->fromRoute('id'),
                'speaker_list' =>$speaker_list
            ));
            $viewModel->setTemplate('resources/session/add');
            return $viewModel;
    
        }else {
            return $this->redirect()->toRoute('Login');
        }
    }
    public function emailAction()
    {
        $this->layout()->setTemplate('layout/additionallayout');
    
        if ($this->getAuthService()->getIdentity()) {
            return $this->redirect()->toUrl('http://www.gmail.com');
        }else {
            return $this->redirect()->toRoute('Login');
        }
    }
    

}